<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MRevisionsController 
    extends MApplicationComponent
    implements MIController
{
    
    public function invoke($uri=null) {
        parent::init();
        
                                $url_manager = new MUrlManager();
        $path = $url_manager->parsePathFromUrl($uri);
        $root = $url_manager->parseRootFromUrl($uri);
        if ($path == false) {
            throw new MException(FXP::t(MConst::PATH_ERROR), MConst::HTTP_CODE_411);;
        }
        $path  = "/" . $path;
                                $rev_limit = 10;
        if (isset($_REQUEST["rev_limit"]) != false) {
            $rev_limit = $_REQUEST["rev_limit"];
        }
        $rev_limit = $rev_limit <= 1000 ? $rev_limit : 1000;
                                $locale = "bytes";
        if (isset($_REQUEST["locale"])) {
            $locale = $_REQUEST["locale"];
        }
                                        $callback = NULL;
        
                        $user   = MUserManager::getInstance()->getCurrentUser();
        $device = MUserManager::getInstance()->getCurrentDevice();
        
        $user_id          = $user->user_id;
        $user_nick        = $user->user_name;
        $user_device_id = $device->device_id;
                                $file_detail = MFiles::queryAllFilesByPath("/". $user_id. $path);
        if ($file_detail === false || count($file_detail) == 0) {
            throw new MException(FXP::t(MConst::NOT_FOUND), MConst::HTTP_CODE_404);
        }
        
                if ($file_detail[0]["file_type"] != 0) {
            throw new MException(FXP::t("Not Acceptable"), MConst::HTTP_CODE_406);
        }
        
        $file_meta = MFileMetas::queryFileMeta("/". $user_id. $path, MConst::VERSION);
        if ($file_meta == false || empty ( $file_meta )) {
            throw new MFilesException ( FXP::t ( MConst::INTERNAL_SERVER_ERROR ), MConst::HTTP_CODE_500 );
        }
                                $versions = unserialize($file_meta[0]["meta_value"]);
        $count = 1;                                 $response = array();
        foreach ($versions as $k => $v){
            $var                 = array();
            $var["rev"]          = strval($v["version_id"]);
            $var["revision"]     = (int)$v["version_id"];
            $var["bytes"]        = 0;
            $var["size"]         = "0 bytes";
            $var["thumb_exists"] = false;
            $var["modified"]     = MUtils::formatIntTime(microtime(true) * 10000);
            $var["mime_type"]    = MConst::DEFAULT_FILE_MIME_TYPE;
            $var["path"]         = $path;
            $var["is_dir"]       = false;
            $var["root"]         = $root;
            if ($v["type"] == MConst::DELETE) {
                $var["is_deleted"] = true;
            }
                                                $file_version = MFileVersions::queryFileVersionByID($v["version_id"]);
            if ($file_version == false || count($file_version) == 0) {
                $var["is_deleted"] = true;
            } else {
                $var["bytes"]        = (int)$file_version[0]["file_size"];
                $var["size"]         = MUtils::getSizeByLocale($locale, $file_version[0]["file_size"]);
                $var["thumb_exists"] = $this->isExistThumbnail($file_version[0]["file_size"], $file_version[0]["mime_type"]);
                $var["modified"]     = MUtils::formatIntTime($file_version[0]["updated_at"]);
                $var["mime_type"]    = is_null($file_version[0]["mime_type"]) ? $var["mime_type"] : $file_version[0]["mime_type"];
            }
            
            array_push($response, $var);
            if ($count >= $rev_limit) {
                break;
            }
            $count += 1;
        }
        echo json_encode($response);
    }
    
    public function isExistThumbnail($size, $mime_type) {
        if ($size > MConst::MAX_IMAGE_SIZE || $size <= 0) {
            return false;
        }
        
        foreach (MThumbnailBase::$_support_types as $value) {
            if ($value == $mime_type) {
                return true;
            }
        }
        return false;
    }
}
?>